package com.mx.zb.mng.ctrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.Note;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.dao.NoteMapper;
import com.mx.zb.mng.utils.AjaxResult;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.db2.Page;
import com.mx.zb.mng.utils.db2.PageHelper;
import com.mx.zb.mng.utils.ex.RestAnnotation;

/***
 * 
 * @author zd
 *
 */
@RestController
@RestAnnotation
@RequestMapping("/a/note")
public class NoteRestCtrl {

	@Autowired
	NoteMapper noteMapper;
	@Autowired
	AgentMapper agentMapper;

	@RequestMapping(value = { "/reader/tocheck" }, method = { RequestMethod.POST })
	@ResponseBody
	public AjaxResult toNoteCheck(Note a, Model model) {
		List<Note> noteList = noteMapper.findbyParams(a);
		model.addAttribute("noteList", noteList);
		return new AjaxResult().success(noteList);
	}

	@RequestMapping(value = { "/reader" }, method = { RequestMethod.DELETE })
	@ResponseBody
	@Transactional
	public AjaxResult deleteNote(Model model, @RequestBody Note o) {
		noteMapper.deleteByPrimaryKey(o.getId());
		return new AjaxResult().success();
	}

	@RequestMapping(value = { "/readers" },method= {RequestMethod.GET})
	@ResponseBody
	@Transactional
	public AjaxResult toNote() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		Note no = new Note();
		String search = ContextThread.get().getSearch();
		Agent agt = new Agent();
		//
		boolean isZongBu = false;
		
		//看是不是总部
		Agent a = agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());
		Date date3=null;
		 if(a!=null) {
			if("总部".equals(a.getType())) {
				isZongBu = true;
			}else {
				no.setTarget("代理商");
				no.setCreatDate1(a.getCreateDate());
				date3=a.getCreateDate();
				isZongBu = false;
			}
		 }
		 PageHelper.startPage();
		 Page<Note> pnPages = new Page<Note>();
		if (search == null || search == "") { // 表示首次加载
			// agt.setLevelCode("0001");
			// agt = agentMapper.findAgent(agt);
		// 	no.setAgentId(ContextThread.get().getAgentId()); // 
			// no.setTarget("代理商");
			//查找所有的通知，因为只有总部能添加
			pnPages = noteMapper.queryPage(no);
		}
		if (search != null && search != "") {// 表示查询
			try {
				no = JSON.parseObject(search, Note.class);
				if (no.getCreateDate1() != null && !"".equals(no.getCreateDate1().toString())) {
					no.setCreateDate(sdf.parse(no.getCreateDate1().toString()));
				}
				if (no.getCreateDate2() != null && !"".equals(no.getCreateDate2().toString())) {
					no.setUpdateDate(sdf.parse(no.getCreateDate2().toString()));
				}
			//	no.setAgentId(ContextThread.get().getAgentId());

				/*if ("3".equals(no.getSource())) {// 自己添加的
					pnPages = noteMapper.queryPage(no);
				} else if ("2".equals(no.getSource())) { // 分销商下方
					// 代理商
					Agent ag = new Agent();
					ag = agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());
					String lcode = ag.getLevelCode().substring(0, ag.getLevelCode().length() - 4); // 父级的
					if (lcode.isEmpty()) {
						lcode = "0001";
					}
					agt.setLevelCode(lcode);
					agt = agentMapper.findAgents(agt);
					if (agt != null) {
						// 查找上级写的公告
						no.setAgentId(agt.getId()); //
						no.setTarget("代理商");
						pnPages = noteMapper.queryPage(no);
					}
				} else {}*/
				if(isZongBu) {
					pnPages = noteMapper.queryPage(no);
				}else {
					//agt.setLevelCode("0001");
					//agt = agentMapper.findAgents(agt);
					//no.setAgentId(agt.getId()); //
					no.setTarget("代理商");
					no.setCreatDate3(date3);
					pnPages = noteMapper.queryPage(no);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return new AjaxResult().success(pnPages);
	}

}
